package com.topfinance.frame.util;

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlHelper {

	/**
	 * String格式的XML转Document
	 * 
	 * @param xml
	 * @param charSet
	 *            字符集编码设置 如：GBK
	 * @return Document
	 * @throws Exception
	 */
	public static Document buildDoc(String xml, String charSet) throws Exception {
		InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
		SAXReader reader = new SAXReader();
		InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charSet);
		Document document = reader.read(inputStreamReader);
		inputStreamReader.close();
		return document;
	}

	/**
	 * Document格式的XML转String
	 * 
	 * @param document
	 * @param charSet字符集编码设置
	 * @return String
	 * @throws Exception
	 */
	public static String buildXML(Document document, String charSet) throws Exception {
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding(charSet);
		ByteArrayOutputStream fos = new ByteArrayOutputStream();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, charSet));
		XMLWriter writer = new XMLWriter(bw, format);
		writer.write(document);
		bw.close();
		String restr = fos.toString();
		fos.close();
		return restr;
	}

	public static org.w3c.dom.Document toW3CDocument(org.dom4j.Document d4doc) {
		org.dom4j.io.DOMWriter d4Writer = new org.dom4j.io.DOMWriter();
		try {
			return d4Writer.write(d4doc);
		} catch (DocumentException e) {
			// logger.warn("can't cast dom4jDocument to W3CDocument", e);
			return null;
		}
	}

	/**
	 * org.dom4j.Document -> org.w3c.dom.Document
	 * 
	 * @param doc
	 *            Document(org.dom4j.Document)
	 * @throws Exception
	 * @return Document
	 */
	public static org.w3c.dom.Document parse(Document doc) throws Exception {
		if (doc == null) {
			return (null);
		}
		java.io.StringReader reader = new java.io.StringReader(doc.asXML());
		org.xml.sax.InputSource source = new org.xml.sax.InputSource(reader);
		javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory = javax.xml.parsers.DocumentBuilderFactory
				.newInstance();
		javax.xml.parsers.DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		return (documentBuilder.parse(source));
	}

	/**
	 * org.w3c.dom.Document -> org.dom4j.Document
	 * 
	 * @param doc
	 *            Document(org.w3c.dom.Document)
	 * @return Document
	 */
	public static Document parse(org.w3c.dom.Document doc) throws Exception {
		if (doc == null) {
			return (null);
		}
		org.dom4j.io.DOMReader xmlReader = new org.dom4j.io.DOMReader();
		return (xmlReader.read(doc));
	}
}
